package com.ruoyi.project.student.controller;


import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.student.domain.Student;
import com.ruoyi.project.student.domain.param.StudentDelParam;
import com.ruoyi.project.student.domain.param.StudentListParam;
import com.ruoyi.project.student.domain.vo.StudentListVo;
import com.ruoyi.project.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/student")
public class StudentController extends BaseController {

    @Autowired
    public StudentService studentService;

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('student:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentListParam param) {
        startPage();
        List<StudentListVo> list = studentService.selectStudentList(param);
        return getDataTable(list);
//        return list;
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('student:add')")
    @PostMapping("/add")
    public AjaxResult addStudent(@Validated @RequestBody Student student){
        boolean isSuccess;
        try {
            isSuccess = studentService.save(student);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return success(isSuccess);
    }

    /**
     * 编辑用户
     */
    @PreAuthorize("@ss.hasPermi('student:edit')")
    @PostMapping("/edit")
    public AjaxResult editStudentById(@Validated @RequestBody Student student){
        try{
            Boolean isSuccess = studentService.updateById(student);
            return success(isSuccess);
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('student:delete')")
    @PostMapping("/delete")
    public AjaxResult delete(@Validated @RequestBody StudentDelParam param){
        try{
            Boolean isSuccess = studentService.removeById(param.getId());
            return success(isSuccess);
        }catch (Exception e){
            return error(e.getMessage());
        }
    }
}
